/*
 *   linux time api study
 *   
 */
#include <sys/time.h>
#include <stdio.h>
#include <time.h>
int main()
{
    int ret;
    struct timeval time_val;
    ret = gettimeofday(&time_val, NULL);
    if (0 != ret)
    {
        perror("get time failed\n");
        exit(-1);
    }

    printf("ctime:%s\n", ctime(&time_val.tv_sec));

    struct tm *tm;
    tm = localtime(&time_val.tv_sec);
    printf("local,astime:%s\n", asctime(tm));

    // tm = gmtime(&time_val.tv_sec);
    // printf("gm, astime:%s\n", asctime(tm));
    
    clock_t clk1 = clock();
    printf("clk1:%d\n", clk1);
    char buf[64] = {0};
    memset(buf, 0, sizeof buf);
    strftime(buf, sizeof buf, "%Y:%M:%d--- %X", tm);
    printf("strtime:  %s\n",buf);
    clock_t clk2 = clock();
    printf("clk2:%d\n",clk2);

    printf("duration second:%f\n",(clk2-clk1)/CLOCKS_PER_SEC);

    struct timespec ts;
    clock_gettime(CLOCK_REALTIME,&ts);
    return 0;
}